package com.nf.health.app.inquiry.docdetail;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.text.TextUtils;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.http.Act;
import com.nf.health.app.inquiry.adapter.DoctorCommentListAdapter;
import com.nf.health.app.models.DoctorCommnetVo;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 查看全部的医生的全部评论
 * @author yyh
 * @date 2015-11-4 上午9:57:30
 */
public class DocCommentDiscussActivity extends BaseActivity {

    @Bind(R.id.rlv_doc_conmment_list)
    DragListView rlv_doc_conmment_list;

    @Bind(R.id.swipe_doc_list_layout)
    SwipeRefreshLayout swipe_doc_list_layout;

    @Bind(R.id.titlebar_title_tv)
    TextView titlebar_title_tv;

    private DoctorCommentListAdapter docCommentListRcyAdapter;
    private int  pageNo = 1;
    private int pageSize  = 20;
    private String doctorId= "2016010816545959080910";
    private List<DoctorCommnetVo> doctorCommnetVoList = new ArrayList<>();
    private boolean isPullToRefreshed = true;//是不是上拉刷新还是下拉加载
    private boolean mIsRefreshing = false;//是否正处于正在下拉刷新过程中
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_doccommentdiscuss);
        ButterKnife.bind(this);
        initView();
        loadBaseDocDetail(pageNo);
    }
    private void loadBaseDocDetail(int indexPage){
        doctorId = getIntent().getStringExtra(Constants.DOCTOR_ID);
        if(TextUtils.isEmpty(doctorId)){
            return ;
        }
        provider.requestDoctorDetailComment3(indexPage + "", pageSize + "", doctorId, Act.Flag.DOC_DETAIL_COMMEMNT);
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){

            case Act.Flag.DOC_DETAIL_COMMEMNT:
                rlv_doc_conmment_list.completeLoadMore();
                List<DoctorCommnetVo> suboctorCommnetVoList = (List<DoctorCommnetVo>) object;
                if(isPullToRefreshed){
                    doctorCommnetVoList.clear();
                    doctorCommnetVoList.addAll(suboctorCommnetVoList);
                    mIsRefreshing = false;
                    if(suboctorCommnetVoList.size() < pageSize){
                        rlv_doc_conmment_list.setRefreshableAndLoadMoreable(false,false);
                    }else{
                        rlv_doc_conmment_list.setRefreshableAndLoadMoreable(false, true);
                    }
                }else{
                    doctorCommnetVoList.addAll(suboctorCommnetVoList);
                    if(suboctorCommnetVoList.size() < pageSize){
                        rlv_doc_conmment_list.setRefreshableAndLoadMoreable(false,false);
                    }else{
                        rlv_doc_conmment_list.setRefreshableAndLoadMoreable(false,true);
                    }
                }
                swipe_doc_list_layout.setRefreshing(false);
                docCommentListRcyAdapter.setDoctorCommentData(doctorCommnetVoList);
                docCommentListRcyAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
        switch (actionName){
            case Act.Flag.DOC_DETAIL:
                break;
            case Act.Flag.DOC_DETAIL_COMMEMNT:
                break;
        }
    }

    private void initView() {
        titlebar_title_tv.setText("医生评价");
        docCommentListRcyAdapter =  new DoctorCommentListAdapter(DocCommentDiscussActivity.this,doctorCommnetVoList);
        rlv_doc_conmment_list.setAdapter(docCommentListRcyAdapter);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        //initRecycleListener();
        //过来的时候就
        /*swipe_doc_list_layout.setProgressViewOffset(false, 0,  (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        swipe_doc_list_layout.setRefreshing(true);*/

        swipe_doc_list_layout.setColorSchemeResources(R.color.theme_color, android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light);
        swipe_doc_list_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isPullToRefreshed = true;
                mIsRefreshing = true;
                pageNo = 1;
                loadBaseDocDetail(pageNo);
            }
        });
        rlv_doc_conmment_list.setRefreshableAndLoadMoreable(false,false);
        rlv_doc_conmment_list.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {  }
            @Override
            public void onLoadMore() {
                isPullToRefreshed = false;
                pageNo++;
                loadBaseDocDetail(pageNo);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
   /* public void initRecycleListener(){
        rlv_doc_conmment_list.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (mIsRefreshing) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
        );
    }*/
}
